var sumPageNumber = 1;
var currPageNumber = 1;
var myTeacherId = "";
var myParentTitle = "";

function searchVideoGetTeacherId(data) {
    // 获取老师Id
    var teacher = JSON.parse(data);
    var teacherName = teacher['name'];
    $.ajax({
        url: "/getParentVideoSumNumberByTeacherId?teacherId="+myTeacherId,
        type: "GET",
        cache: false,
        async: false,
        success: function (data) {
            var sumNumber = JSON.parse(data).sumNumber;
            document.getElementById("teacherInfo").innerHTML = "<div class=\"sidebox-header mooc-header\">\n" +
                "<h4 class=\"sidebox-title\">课程教师</h4>\n" +
                "</div>\n" +
                "<div class=\"sidebox-body mooc-content\">\n" +
                "<a>\n" +
                "<img src=\"../img/Linux&c.png\">\n" +
                "</a>\n" +
                "<div class=\"mooc-info\">\n" +
                "<div class=\"name\"><strong id=\"courseTeacher\">" + teacherName + "</strong> </div>\n" +
                "\n" +
                "<div class=\"courses\">共发布过<strong id=\"coursesCount\">" + sumNumber + "</strong>门视频课程</div>\n" +
                "</div>\n" +
                "</div>";
        },
        error: function() {
            alert("ajaxError: 函数getSumVideoPageNumber请求失败");
        }

    });
}

function getVideoByWebUrl(videoUrl) {
    $.ajax({
        url: "/getVideoByWebUrl?webUrl="+videoUrl,
        type: "GET",
        cache: false,//设置不缓存
        success: function (data) {
            data=JSON.parse(data);
            var result = data.result;
            if (result!=="success"){
                alert(result);
                return;
            }
            var video = data.videoList[0];
            myTeacherId = video.teacherId;
            myParentTitle=video.parentTitle;
            var title="<h2 class=\"\">"+myParentTitle+"-"+ video.childTitle+"<!--&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"glyphicon glyphicon-plus\"></span>--></h2>";
            document.getElementById("title").innerHTML=title;
            getSumChildVideoPageNumber();
            getTeacherInfo();
        },
        error:function () {
        }
    });
}

function getSumChildVideoPageNumber() {
    var URL = "/getChildVideoSumNumber?teacherId="+myTeacherId+"&parentTitle="+myParentTitle;
    $.ajax({
        url: URL,
        type: "GET",
        cache: false,
        async: false,
        success: function(data) {
            data = JSON.parse(data);
            var result = data.result;
            if (result !== "success") {
                alert("无法获取视频总页数");
            }
            sumPageNumber = data.sumNumber;
            showChildVideo();

        },
        error: function() {
            alert("ajaxError: 函数getSumVideoPageNumber请求失败");
        }

    });
}
function showChildVideo() {
    var URL = "/getVideoByTeacherIdAndParentTitle?parentTitle=" + myParentTitle + "&teacherId=" + myTeacherId + "&pageNumber=" + currPageNumber;
    $.ajax({
        url: URL,
        type: "GET",
        cache: false,
        async: false,
        success: showChildVideoSuccess,
        error: function() {
            alert("ajaxError: 函数getSumVideoPageNumber请求失败");
        }

    });
}

function showChildVideoSuccess(data) {
    data = JSON.parse(data);
    var result = data.result;
    if (result !== "success") {
        alert(result);
        return;
    }
    var videoList = data.videoList;
    var length = videoList.length;
    var str="<li class=\"nav-header\">\n" +
        "本课程视频合集\n" +
        "</li>";

    for (var count = 0; count < length; count++) {
        str += "<li >\n" +
            "<a href=\"index.html?videoUrl="+videoList[count].videoWebUrl+"&imgWebUrl="+videoList[count].childImgWebUrl+"\"><img src=\""+videoList[count].childImgWebUrl+"\" width=\"50\" height=\"50\"/> &nbsp;" +videoList[count].childTitle+"</a>\n" +
            "</li>";
    }
    document.getElementById("my-video-list").innerHTML = str;
    showNumber();
}

function showNumber() {
    if (sumPageNumber !== 0) {
        var str = "<li><a onclick=\"jumpToPreviousPage()\">&laquo;</a></li>";
        str += "<li><a onclick=\"jumpToNextPage()\">&raquo;</a></li>";
        document.getElementById("page-number").innerHTML = str;
    }
}

function jumpToPreviousPage() {
    if (currPageNumber !== 1) {
        currPageNumber = currPageNumber - 1;
        showChildVideo();
    }
}
function jumpToNextPage() {
     if (currPageNumber !== sumPageNumber) {
         currPageNumber = currPageNumber + 1;
         showChildVideo();
     }
}


function isLogin(){
    $.ajax({
        url: "/get_user",
        type: "POST",
        cache: false,//设置不缓存
        success: changeLogin,
        error:function () {
        }
    });
}
function changeLogin(data) {
        var user = JSON.parse(data);
        userName = user['name'];
        var str = "<ul class='nav navbar-nav'>" +
            "<li class='dropdown'>" +
            "<a href='javascript:void(0);' class='dropdown-toggle' data-toggle='dropdown'>" +
            "我的课程" +
            "</a>" +
            "<ul class='dropdown-menu'>" +
            "<li><a class='' href=''>" + data + "</a></li>" +
            "</ul>" +
            "</li>" +
            "</ul>" +
            "<ul class='nav navbar-nav'>" +
            "<li class='dropdown'>" +
            "<a href='javascript:void(0);' class='dropdown-toggle' data-toggle='dropdown'>" +
            "欢迎您，" + userName +
            "</a>" +
            "<ul class='dropdown-menu'>" +
            "<li><a class='' href='/home/index.html' >我的主页</a></li>" +
            "<li><a class='' href='/add_user/index.html' >添加用户</a></li>" +
            "<li><a class='' href='/back_stage/index.html' >后台管理</a></li>" +
            "<li><a class='' href='' onclick='signOut(); return false;' data-toggle='modal' data-target='#signOutModel'>退出登录</a></li>" +
            "</ul>" +
            "</li>" +
            "</ul>";
        document.getElementById("userInfo").innerHTML = str;
}

function getTeacherInfo(){
    $.ajax({
        url: "/get_teacher_info",
        type: "POST",
        cache: false,
        data: {
            "teacherId" : myTeacherId
        },
        success: searchVideoGetTeacherId,
        error: function () {
            alert("ajaxError: 函数getTeacherInfo请求失败");
        }
    });
}


$(document).ready(function () {
    isLogin();
    var videoUrl = location.href.split("?")[1].split("&")[0].split("=")[1];
    videoUrl = decodeURI(videoUrl);
    var imgWebUrl = location.href.split("&")[1].split("=")[1];
    imgWebUrl = decodeURI(imgWebUrl);
    var player = new Clappr.Player({source: videoUrl, poster: imgWebUrl,parentId: "#player",width:"800",height: "500"});
    getVideoByWebUrl(videoUrl);

});